<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\MallProduct;

/**
 * MallProductSearch represents the model behind the search form of `backend\models\MallProduct`.
 */
class MallProductSearch extends MallProduct
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'aid', 'mid', 'cid', 'sort', 'count_sale', 'is_up', 'up_time', 'update_time', 'create_time', 'status', 'parent_id'], 'integer'],
            [['title', 'img', 'imgs', 'detail', 'detail_text'], 'safe'],
            [['old_price', 'freight'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = MallProduct::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'aid' => $this->aid,
            'mid' => $this->mid,
            'cid' => $this->cid,
            'old_price' => $this->old_price,
            'freight' => $this->freight,
            'sort' => $this->sort,
            'count_sale' => $this->count_sale,
            'is_up' => $this->is_up,
            'up_time' => $this->up_time,
            'update_time' => $this->update_time,
            'create_time' => $this->create_time,
            'status' => $this->status,
            'parent_id' => $this->parent_id,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'img', $this->img])
            ->andFilterWhere(['like', 'imgs', $this->imgs])
            ->andFilterWhere(['like', 'detail', $this->detail])
            ->andFilterWhere(['like', 'detail_text', $this->detail_text]);

        return $dataProvider;
    }
}
